package com.miyake.client.datamanage;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.googlecode.gchart.client.GChart;
import com.miyake.client.DataServiceAsync;
import com.miyake.client.tools.ClickableLabel;
import com.miyake.client.tools.OkCancelDialog;
import com.miyake.client.tools.ShowDialog;
import com.miyake.client.tools.SmallButton;

public class FolderPage extends Page {

	public FolderPage(NodeProperty node, VerticalPanel panel) {
		super(node, panel);
	}

	@Override
	protected Widget getContent(NodeProperty node, DataServiceAsync service) {
		VerticalPanel panel = new VerticalPanel();

		if (canEdit()) {
			panel.add(createToolBar());
			panel.add(new SecurityLevelWidget(node, service));
		}

		ShowChildList2 list = new ShowChildList2(node, panel, updateHandler);
//		list.setType(NodeProperty.PLOT);
		list.show();
		
//		new ShowChildList(node, panel, updateHandler);
		
		return panel;
	}

	protected void deleteNode(String key) {
		getService().deleteNode(key, new AsyncCallback<Void>() {

			@Override
			public void onFailure(Throwable caught) {
				Window.alert(caught.getMessage());
			}

			@Override
			public void onSuccess(Void result) {
				fireNodeUpdateEvent("");
			}
		
		});
	}

	private HorizontalPanel createToolBar() {
		HorizontalPanel toolBar = new HorizontalPanel();

		toolBar.add(new SmallButton("Create Folder", new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				if (createHandler != null) {
					createHandler.onNewFolder();
				}
			}
		}));
		toolBar.add(new SmallButton("Import Data", new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				if (createHandler != null) {
					createHandler.onNewData();
				}
			}
		}));
		toolBar.add(new SmallButton("Import Image", new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				if (createHandler != null) {
					createHandler.onNewImage();
				}
			}
		}));
		toolBar.add(new SmallButton("Add Text", new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				if (createHandler != null) {
					createHandler.onNewText();
				}
			}
		}));
		return toolBar;
	}

	protected List<NodeProperty> getSelectedNodes(final NodeProperty node, final Grid grid) {
		List<NodeProperty> ret = new ArrayList<NodeProperty>();
		int row = 1;
		for (NodeProperty child : node.getChildren()) {
			if ( ((CheckBox)grid.getWidget(row++, 1)).getValue().equals(true)) {
				ret.add(child);
			}
		}
		
		return ret;
	}

}
